package cn.iocoder.yudao.module.product.controller.admin.cylindertype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.cylindertype.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.cylindertype.CylinderTypeDO;
import cn.iocoder.yudao.module.product.service.cylindertype.CylinderTypeService;

@Tag(name = "管理后台 - 钢瓶类型")
@RestController
@RequestMapping("/product/cylinder-type")
@Validated
public class CylinderTypeController {

    @Resource
    private CylinderTypeService cylinderTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建钢瓶类型")
    @PreAuthorize("@ss.hasPermission('product:cylinder-type:create')")
    public CommonResult<Long> createCylinderType(@Valid @RequestBody CylinderTypeSaveReqVO createReqVO) {
        return success(cylinderTypeService.createCylinderType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新钢瓶类型")
    @PreAuthorize("@ss.hasPermission('product:cylinder-type:update')")
    public CommonResult<Boolean> updateCylinderType(@Valid @RequestBody CylinderTypeSaveReqVO updateReqVO) {
        cylinderTypeService.updateCylinderType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除钢瓶类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:cylinder-type:delete')")
    public CommonResult<Boolean> deleteCylinderType(@RequestParam("id") Long id) {
        cylinderTypeService.deleteCylinderType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得钢瓶类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:cylinder-type:query')")
    public CommonResult<CylinderTypeRespVO> getCylinderType(@RequestParam("id") Long id) {
        CylinderTypeDO cylinderType = cylinderTypeService.getCylinderType(id);
        return success(BeanUtils.toBean(cylinderType, CylinderTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得钢瓶类型分页")
    @PreAuthorize("@ss.hasPermission('product:cylinder-type:query')")
    public CommonResult<PageResult<CylinderTypeRespVO>> getCylinderTypePage(@Valid CylinderTypePageReqVO pageReqVO) {
        PageResult<CylinderTypeDO> pageResult = cylinderTypeService.getCylinderTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CylinderTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出钢瓶类型 Excel")
    @PreAuthorize("@ss.hasPermission('product:cylinder-type:export')")
    @OperateLog(type = EXPORT)
    public void exportCylinderTypeExcel(@Valid CylinderTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CylinderTypeDO> list = cylinderTypeService.getCylinderTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "钢瓶类型.xls", "数据", CylinderTypeRespVO.class,
                        BeanUtils.toBean(list, CylinderTypeRespVO.class));
    }


    @GetMapping("/list")
    @Operation(summary = "获得钢瓶类型列表")
    public CommonResult<List<CylinderTypeRespVO>> getCylinderTypeList() {
        List<CylinderTypeDO> list = cylinderTypeService.getCylinderTypeList();
        return success(BeanUtils.toBean(list, CylinderTypeRespVO.class));
    }
}